//
//  CZComposeToolBar.swift
//  WeiBo
//
//  Created by Apple on 16/9/8.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  UIStackView不具备渲染功能，颜色依赖于子控件的颜色
//  撰写按钮的toolBar
class CZComposeToolBar: UIStackView {
    
    //  点击toolBar按钮执行的闭包
    var didSelectedToolBarButtonClosure: ((CZComposeToolBarButtonType) -> ())?
    
    //  表情按钮
    var emoticonButton: UIButton?
    
    //  toolbar枚举类型
    enum CZComposeToolBarButtonType: Int {
        //  图片
        case picture = 0
        //  @
        case mention = 1
        //  # 话题
        case trend = 2
        //  表情
        case emotion = 3
        //  加号
        case add = 4
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件
    private func setupUI() {
        
        
        //  布局方式
        axis = .horizontal
        //  子控件布局方式
        distribution = .fillEqually
        
        //  添加控件
        addChildButton(imageName: "compose_toolbar_picture", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emotion)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    //  添加子按钮, @discardableResult 可以忽略返回结果
    @discardableResult private func addChildButton(imageName: String, type: CZComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        //  枚举的原始值作为tag值使用
        button.tag = type.rawValue
        //  添加点击事件
        button.addTarget(self, action: #selector(btnAction(button:)), for: .touchUpInside)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        
        //  不要高亮点击效果
        button.adjustsImageWhenHighlighted = false
        
        addArrangedSubview(button)
        return button
    }
    
    // MARK:    -点击事件
    @objc private func btnAction(button: UIButton) {
        //  通过原始值或者枚举
        let type = CZComposeToolBarButtonType(rawValue: button.tag)!
        //  执行点击toolbar按钮的闭包
        didSelectedToolBarButtonClosure?(type)
        
    }
    
    //  根据表情键盘标记，显示不同的表情icon
    func showEmoticonIcon(isEmoticon: Bool) -> Void {
        //  compose_emoticonbutton_background_highlighted
        var imageName = "compose_emoticonbutton_background"
        //  如果是表情键盘，那么显示键盘icon
        if isEmoticon {
            imageName = "compose_keyboardbutton_background"
        }
        //  如果是系统键盘，那么显示表情icon
        
        emoticonButton?.setImage(UIImage(named: imageName), for: .normal)
        emoticonButton?.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
    }

}
